package utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import Entity.ToolsDBEntity;
import android.util.Xml;


public class XmlToolsParser {
	 private static final String ns = null;
	 public ToolsDBEntity parse(InputStream in) throws XmlPullParserException, IOException {   
		 try {
         XmlPullParser parser = Xml.newPullParser();
         parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);       
         parser.setInput(in , null);
         parser.nextTag();       
         return readToolsXml(parser); 
	 } finally {
         in.close();
     }
 }
	 
	 private ToolsDBEntity readToolsXml(XmlPullParser parser) throws XmlPullParserException, IOException {
		 parser.require(XmlPullParser.START_TAG, ns, "data");
		 ToolsDBEntity tools = new ToolsDBEntity();
		 while (parser.next() != XmlPullParser.END_TAG) {
			 if (parser.getEventType() != XmlPullParser.START_TAG) {
	                continue;
	            }
			 String name = parser.getName();
			 if (name.equals("toolgroupcode")) {
				 tools.setToolgroupcode(readToolgroupcode(parser)) ;
				 
			 }
			 else if  (name.equals("tools")) {
				 tools.setTools(readTools(parser)) ;
				 
				 
			 }
	            
	            else{
	            	XmlTemplateParser.skip(parser);
	            }
		 }
		 return tools;
	 }
	 

	 private String  readToolgroupcode (XmlPullParser parser)throws IOException, XmlPullParserException {
		 parser.require(XmlPullParser.START_TAG, ns, "toolgroupcode");
		 String text = XmlTemplateParser.readText(parser);
	      parser.require(XmlPullParser.END_TAG, ns, "toolgroupcode");
		 return  text;
		 
	 }
	 
	 private ArrayList<MyFifthSet> readTools(XmlPullParser parser) throws IOException, XmlPullParserException {
	    	ArrayList<MyFifthSet> list= new ArrayList<MyFifthSet>();
	        parser.require(XmlPullParser.START_TAG, ns, "tools");
	        while (parser.next() != XmlPullParser.END_TAG) {
	            if (parser.getEventType() != XmlPullParser.START_TAG) {
	                continue;
	            }
	            String name = parser.getName();
	            if (name.equals("tool")) {
	            	 list.add(readTool(parser)) ;}
	            
	        	 else {
	        		 XmlTemplateParser.skip(parser);
	            }
	        }

	        return list;
	    }
	 private OrderedFifthSet<String, String ,String ,String ,String> readTool (XmlPullParser parser)throws IOException, XmlPullParserException {
		 String key = null ; 
		 String value1 = null ; 
		 String value2 = null ; 
		 String value3 = null ; 
		 String value4 = null ; 
		 parser.next();
	     parser.next();
		 parser.require(XmlPullParser.START_TAG, ns, "toolname");
	      key= XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "toolname");
	        parser.next();
	        parser.next();
	        parser.require(XmlPullParser.START_TAG, ns, "toolcode");     
	        value1 = XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "toolcode");
	        parser.next();
	        parser.next();
	        parser.require(XmlPullParser.START_TAG, ns, "toolunit");     
	        value2 = XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "toolunit");
	        parser.next();
	        parser.next();
	        parser.require(XmlPullParser.START_TAG, ns, "toolunitcode");     
	        value3 = XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "toolunitcode");
	        parser.next();
	        parser.next();
	        parser.require(XmlPullParser.START_TAG, ns, "toolprice");     
	        value4 = XmlTemplateParser.readText(parser);
	        parser.require(XmlPullParser.END_TAG, ns, "toolprice");
	        parser.next();
	        parser.next();
	        OrderedFifthSet <String, String, String , String , String> tool = new  OrderedFifthSet<String, String, String ,String ,String>(key,value1,value2,value3,value4);
		 return  tool;

	 }
	 
	 
	  
	
}
